﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Version3.SharePoint;
using SampleRenderer;

namespace TestConsole
{
    //This test console demonstrates a few ways to use the freeze dried data site column.
    //It assumes that there is a web at http://localhost (of course you can change that)
    //And a list or library named "Freeze Dried Test List" with four freeze dried columns on it
    //called "Data One" and "Data Two", "Data three" and "Data four"

    //The post build event in the Version3.FreezeDriedSiteColumn project installs the
    //site column into your local SharePoint installation

    class Program
    {


        static void Main(string[] args)
        {

            Random randomCount = new Random(DateTime.Now.Millisecond);
            using (SPSite newSite = new SPSite("http://localhost/"))
            {
                using (SPWeb newWeb = newSite.RootWeb)
                {

                    SPList testList = newWeb.Lists["Freeze Dried Test List"];

                    //first lets ready the currrent items in and list details from them
                    foreach (SPListItem existingItem in testList.Items)
                    {
                        Console.WriteLine("Data from " + existingItem.Title);
                        //use the simple "object" cast technique
                        FreezeDriedDataFieldValue freezeDriedObject = (FreezeDriedDataFieldValue)existingItem["Data One"];
                        if (null != freezeDriedObject)
                        {
                            Console.Write(".." + freezeDriedObject.DisplayText);
                            TestStruct testStruct = (TestStruct)freezeDriedObject.Value;
                            Console.WriteLine(".." + testStruct.Id.ToString("B") + "  " + testStruct.When.ToString());
                        }

                        //use the generic cast technique on the other column, just to demonstrate
                        Version3.SharePoint.Generic.FreezeDriedDataFieldValue<DifferentTestStruct> genericFreezeDriedObject = new Version3.SharePoint.Generic.FreezeDriedDataFieldValue<DifferentTestStruct>(existingItem["Data Two"]);
                        if (null != freezeDriedObject)
                        {
                            Console.Write(".." + genericFreezeDriedObject.DisplayText);
                            Console.Write(".." + genericFreezeDriedObject.Value.Name);
                            for (int loopIndex = 0; loopIndex < genericFreezeDriedObject.Value.ArrayOfTestStruct.Length; loopIndex++)
                            {
                                Console.Write("..->");
                                Console.Write(genericFreezeDriedObject.Value.ArrayOfTestStruct[loopIndex].Id.ToString("B"));
                                Console.Write(" ");
                                Console.WriteLine(genericFreezeDriedObject.Value.ArrayOfTestStruct[loopIndex].When.ToString());

                            }
                        }
                    }

                    //Ok now add an item to the list, does not matter what so we will
                    //just create some data
                    SPListItem newItem = testList.Items.Add();
                    newItem["Title"] = "Added on " + DateTime.Now.ToShortTimeString();
                    TestStruct dataOne = new TestStruct();
                    dataOne.Id = Guid.NewGuid();
                    dataOne.When = DateTime.Now;
                    newItem["Data One"] = new FreezeDriedDataFieldValue(dataOne);

                    DifferentTestStruct dataTwo = new DifferentTestStruct();
                    dataTwo.Name = newItem.Title;
                    dataTwo.ArrayOfTestStruct = new TestStruct[randomCount.Next(20)];
                    for (int loopIndex = 0; loopIndex < dataTwo.ArrayOfTestStruct.Length; loopIndex++)
                    {
                        dataTwo.ArrayOfTestStruct[loopIndex] = new TestStruct();
                        dataTwo.ArrayOfTestStruct[loopIndex].Id = Guid.NewGuid();
                        dataTwo.ArrayOfTestStruct[loopIndex].When = DateTime.Now;
                    }

                    Version3.SharePoint.Generic.FreezeDriedDataFieldValue<DifferentTestStruct> dataFieldTwo = new Version3.SharePoint.Generic.FreezeDriedDataFieldValue<DifferentTestStruct>();
                    dataFieldTwo.Value = dataTwo;
                    newItem["Data Two"] = dataFieldTwo;

                    dataFieldTwo.DisplayText = "Not the Data Type .. ha ha " + DateTime.Now.Second.ToString();
                    newItem["Data Three"] = dataFieldTwo;
                    FileBytesInColumn dataFieldFour = new FileBytesInColumn();
                    dataFieldFour.Name = "Icon";
                    dataFieldFour.OriginalPath = "ver3.ico";
                    System.IO.FileStream fStream = System.IO.File.OpenRead(dataFieldFour.OriginalPath);
                    dataFieldFour.Bytes = new byte[fStream.Length];
                    int bytesRead = fStream.Read(dataFieldFour.Bytes, 0, dataFieldFour.Bytes.Length);
                    newItem["Data Four"] = new FreezeDriedDataFieldValue(dataFieldFour,"Ver3.Ico");
                    newItem.Update();
                }
            }
            Console.Write("Press enter to continue");
            Console.ReadLine();
        }
    }
}
